package com.cliff.joinme.toolkit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.cliff.joinme.toolkit.data.ImageItem;
import com.cliff.joinme.toolkit.data.MediaItem;
import com.cliff.joinme.toolkit.data.MusicItem;
import com.cliff.joinme.toolkit.data.VideoItem;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class MediaStoreHelper {

	private static MediaStoreHelper instance = null;
	
	protected final String SELECTION_MUSIC = MediaStore.Audio.Media.IS_MUSIC+"!=0";
	protected final String SORTORDER_MUSIC = MediaStore.Audio.Media.ALBUM+","+MediaStore.Audio.Media.TRACK;

	private MediaStoreHelper() {
	}

	synchronized public static MediaStoreHelper getInstance() {
		if (null == instance)
			instance = new MediaStoreHelper();
		return instance;
	}

//	private Map<String, ArrayList<MediaItem>> getFolderMap(Cursor c) {
//		Map<String, List<String>> retMap = new HashMap<String, List<String>>();
//		String filePathName = "";
//		String filePath = "";
//		ArrayList<MediaItem> filePathNameList = null;
//
//		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//			filePathName = c.getString(0);
//			filePath = filePathName.substring(0, filePathName.lastIndexOf("/"));
//			filePathNameList = retMap.get(filePath);
//			if (null == filePathNameList) {
//				filePathNameList = new ArrayList<MediaItem>();
//				retMap.put(filePath, filePathNameList);
//			}
//			filePathNameList.add(filePathName);
//		}
//
//		return retMap;
//	}
//
//	public Map<String, List<String>> getImageFolderMap(Context ctx) {
//		String[] projection = new String[] { MediaStore.Images.Media.DATA };
//		ContentResolver resolver = ctx.getContentResolver();
//		Cursor c = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
//				projection, null, null, null);
//		Map<String, List<String>> retMap = getFolderMap(c);
//		c.close();
//		return retMap;
//	}
//
//	public Map<String, List<String>> getMusicFolderMap(Context ctx) {
//		String[] projection = new String[] { MediaStore.Audio.Media.DATA };
//		ContentResolver resolver = ctx.getContentResolver();
//		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
//				projection, null, null, null);
//		Map<String, List<String>> retMap = getFolderMap(c);
//		c.close();
//		return retMap;
//	}
//
//	public Map<String, List<String>> getVideoFolderMap(Context ctx) {
//		String[] projection = new String[] { MediaStore.Video.Media.DATA };
//		ContentResolver resolver = ctx.getContentResolver();
//		Cursor c = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
//				projection, null, null, null);
//		Map<String, List<String>> retMap = getFolderMap(c);
//		c.close();
//		return retMap;
//	}
	
	public int getMusicCount(Context ctx) {
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[0], SELECTION_MUSIC, null, null);
		int ret = c.getCount();
		c.close();
		return ret;
	}

	public int getImageCount(Context ctx) {
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new String[0], null, null, null);
		int ret = c.getCount();
		c.close();
		return ret;
	}

	public int getVideoCount(Context ctx) {
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				new String[0], null, null, null);
		int ret = c.getCount();
		c.close();
		return ret;
	}


	public ArrayList<MediaItem> getImageItemList(Context ctx) {
		String[] projection = new String[] { MediaStore.Images.Media.DATA };
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				projection, null, null, null);
		ArrayList<MediaItem> retList = new ArrayList<MediaItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			ImageItem item = new ImageItem();
			item.filePathName = c.getString(0);
			retList.add(item);
		}
		c.close();
		return retList;
	}

	public ArrayList<MediaItem> getMusicItemList(Context ctx) {
		String[] projection = new String[] { 
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.TITLE,		
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Audio.Media.ALBUM, 
				MediaStore.Audio.Media.ARTIST, 
				MediaStore.Audio.Media.DURATION 
		};
		
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				projection, SELECTION_MUSIC, null, SORTORDER_MUSIC);
		ArrayList<MediaItem> retList = new ArrayList<MediaItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			MusicItem item = new MusicItem();
			item.filePathName = c.getString(0);
			item.title = c.getString(1);
			item.displayName = c.getString(2);
			item.album = c.getString(3);
			item.artist = c.getString(4);
			item.duration= c.getLong(5);
			retList.add(item);
		}
		c.close();
		return retList;
	}

	public ArrayList<MediaItem> getVideoItemList(Context ctx) {
		String[] projection = new String[] { 
				MediaStore.Video.Media.DATA,
				MediaStore.Video.Media.RESOLUTION,
				MediaStore.Video.Media.DURATION
		};
		
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				projection, null, null, null);
		ArrayList<MediaItem> retList = new ArrayList<MediaItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			VideoItem item = new VideoItem();
			item.filePathName = c.getString(0);
			item.resolution = c.getString(1);
			item.duration = c.getLong(2);
			retList.add(item);
		}
		c.close();
		return retList;
	}
	
	public String[] getMusicFileInfo(Context ctx, String paramName[], String filePathName) {
		String retArray[] = null;
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, paramName, MediaStore.Audio.Media.DATA+"=\""+filePathName+"\"", null, null);		
		if (c.getCount() >= 1) {
			c.moveToFirst();
			retArray = new String[paramName.length];
			for (int i = 0; i < paramName.length; i++)
				retArray[i] = c.getString(i);
		}
		c.close();
		return retArray;
	}
}
